/**
 * Created by JiaJia on 2019/3/27 0027.
 */
public class LengthOfLastWord {
    public static void main(String[] args) {
        String str="study hard to get the best offer";
        System.out.println(lengthOfLastWord(str));
    }

    /**
     *
     * 先找到最后一个字符的位置，再找到最后一个空格的位置，两者相减
     *
     */
    public static int lengthOfLastWord(String str){
        int last=str.length()-1;
       /* while(last>=0&&str.charAt(last)==' '){
            last--;
        }*/
        int pos=last-1;
        while (pos>=0&&str.charAt(pos)!=' '){
            pos--;
        }
        return last-pos;

    }
}